/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * UserManaged.java, Jun 5, 2012 HaiPD
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;

import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.hibernate.MstGroup;
import net.luvina.manageuser.hibernate.MstJapan;
import net.luvina.manageuser.hibernate.TblDetailUserJapan;
import net.luvina.manageuser.hibernate.TblUser;
import net.luvina.manageuser.logic.MstGroupLogic;
import net.luvina.manageuser.logic.MstJapanLogic;
import net.luvina.manageuser.logic.TblUserLogic;
import net.luvina.manageuser.util.CommonUtil;
import net.luvina.manageuser.util.ConfigProperties;
import net.luvina.manageuser.util.Message;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Manager action user
 * 
 * @author HaiPD
 * 
 */
@Component("userManaged")
@Scope("request")
public class UserManaged extends UserBaseManaged implements Serializable {
	private static final long serialVersionUID = 1L;
	private Date birthday;
	private int birthdayDay;
	private int birthdayMonth;
	private int birthdayYear;
	private String codeLevel;
	private String email;
	private Date endDate;
	private int endDay;
	private int endMonth;
	private int endYear;
	private String fullName;
	private String fullNameKana;
	private int groupId;
	private String groupName;
	private String loginName;
	private List<String> lsErrorMessage;
	private List<SelectItem> lsItemBirthdayDay;
	private List<SelectItem> lsItemBirthdayMonth;
	private List<SelectItem> lsItemBirthdayYear;
	private List<SelectItem> lsItemEndDay;
	private List<SelectItem> lsItemEndMonth;
	private List<SelectItem> lsItemEndYear;
	private List<SelectItem> lsItemGroup;
	private List<SelectItem> lsItemJapan;
	private List<SelectItem> lsItemStartDay;
	private List<SelectItem> lsItemStartMonth;
	private List<SelectItem> lsItemStartYear;
	private String messageSucess;
	private String nameLevel;
	private String password;
	private String passwordConfirm;
	private Date startDate;
	private int startDay;
	private int startMonth;
	private int startYear;
	private String strBirthday;
	private String strEndDate;
	private String strStartDate;
	private String tel;
	private int total;
	private String totalStr;
	private int userId;

	@Autowired
	private MstGroupLogic groupLogic;
	@Autowired
	private MstJapanLogic japanLogic;
	@Autowired
	private TblUserLogic tblUserLogic;
	@Autowired
	private UserSession userSession;

	/**
	 * Contructor default
	 */
	public UserManaged() {
		this.lsErrorMessage = new ArrayList<String>();
		lsErrorMessage.add("");
	}

	/**
	 * method initPage method run first JSF
	 * 
	 * @param event
	 *            The PhaseEvent object
	 */
	public final void initPage(PhaseEvent event) {
		// begin binding data group
		this.lsItemGroup = this.groupLogic.getListItemGroup(ConfigProperties
				.getData("default_adm003"));
		// end binding data group

		// begin binding data japan
		this.lsItemJapan = this.japanLogic.getListItemJapan(ConfigProperties
				.getData("default_adm003"));
		// end binding data japan

		this.setDefaultValue();

	}

	/**
	 * doConfirm view ADM004
	 * 
	 * @return string the page
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public String doConfirm() {
		this.validate();
		if (this.lsErrorMessage.size() <= 1) {

			for (SelectItem items_japan : lsItemJapan) {
				if (items_japan.getValue().equals(codeLevel)) {
					nameLevel = items_japan.getLabel();
				}
			}

			SelectItem items_group = lsItemGroup.get(groupId);
			groupName = items_group.getLabel();
			BeanUtils.copyProperties(this, userSession);
			return CommonConstant.ADM004;
		}
		return CommonConstant.ADM003;
	}

	/**
	 * validate for validate in managed bean
	 */
	private void validate() {
		if (tblUserLogic.checkExistedEmail(userId, email)) {
			this.lsErrorMessage.add("「メールアドレス」 " + Message.getString("ER003"));
		}
		if (tblUserLogic.checkExistedLoginName(userId, loginName)) {
			this.lsErrorMessage.add("「アカウント名」 " + Message.getString("ER003"));
		}
		if (!password.equals(passwordConfirm)) {
			this.lsErrorMessage.add(Message.getString("ER017"));
		}

		// begin validation birthday, start date, end date
		this.birthday = CommonUtil.toDate(birthdayYear, birthdayMonth,
				birthdayDay);
		;
		if (birthday == null) {
			this.lsErrorMessage.add("「生年月日」 " + Message.getString("ER011"));
		}

		this.startDate = CommonUtil.toDate(startYear, startMonth, startDay);
		if (startDate == null) {
			this.lsErrorMessage.add("「資格交付日」 " + Message.getString("ER011"));
		}

		this.endDate = CommonUtil.toDate(endYear, endMonth, endDay);
		if (endDate == null) {
			this.lsErrorMessage.add("「失効日」 " + Message.getString("ER011"));
		}
		// end validation birthday, start date, end date

		// begin validation start date < end date
		if (startDate.after(endDate)) {
			this.lsErrorMessage.add(Message.getString("ER012"));
		}
		// end validation start date < end date

		// begin validation total when group japan selected
		if (!"".equals(codeLevel) || codeLevel == null) {
			if ("".equals(totalStr)) {
				this.lsErrorMessage.add("「点数」 " + Message.getString("ER001"));
			}
		}
		// end validation total when group japan selected

	}

	/**
	 * back the page ADM003
	 * 
	 * @return string the page
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public String doBack() throws IllegalAccessException,
			InvocationTargetException {
		BeanUtils.copyProperties(this, userSession);
		return CommonConstant.ADM003;
	}

	public String doDone() {

		MstGroup group = new MstGroup(userSession.getGroupId(),
				userSession.getGroupName());
		MstJapan japan = new MstJapan(userSession.getCodeLevel(),
				userSession.getNameLevel());

		birthday = CommonUtil.toDate(userSession.getBirthdayYear(),
				userSession.getBirthdayMonth(), userSession.getBirthdayDay());
		startDate = CommonUtil.toDate(userSession.getStartYear(),
				userSession.getStartMonth(), userSession.getStartDay());
		endDate = CommonUtil.toDate(userSession.getEndYear(),
				userSession.getEndMonth(), userSession.getEndDay());

		total = Integer.parseInt(userSession.getTotalStr());

		TblUser tblUser = new TblUser(group, userSession.getLoginName(),
				userSession.getPassword(), userSession.getFullName(),
				userSession.getEmail(), userSession.getTel(), birthday);

		TblDetailUserJapan tblDetailUserJapan = new TblDetailUserJapan(tblUser,
				japan, startDate, endDate, total);
		
		if (tblUserLogic.createUser(tblUser)
				&& tblUserLogic.createDetailUserJapan(tblDetailUserJapan)) {
			return CommonConstant.ADM002;
		}

		return CommonConstant.SYSTEM_ERROR;
	}

	/**
	 * method setDefaultValue set data default
	 */
	public void setDefaultValue() {
		this.userId = 0;

		// strat binding data cbb birthday
		this.lsItemBirthdayYear = CommonUtil.getListSelectItemYear(0);
		this.lsItemBirthdayMonth = CommonUtil.getListSelectItemMonth();
		this.lsItemBirthdayDay = CommonUtil.getListSelectItemDay();
		// end binding data cbb birthday

		// begin binding data ccb start date
		this.lsItemStartYear = CommonUtil.getListSelectItemYear(0);
		this.lsItemStartMonth = CommonUtil.getListSelectItemMonth();
		this.lsItemStartDay = CommonUtil.getListSelectItemDay();
		// end binding data cbb start date

		// begin binding data cbb end date
		this.lsItemEndYear = CommonUtil.getListSelectItemYear(1);
		this.lsItemEndMonth = CommonUtil.getListSelectItemMonth();
		this.lsItemEndDay = CommonUtil.getListSelectItemDay();
		// end binding date cc end date
	}

	/**
	 * method doRegist display screen ADM003
	 * 
	 * @return String ADM003
	 */
	public String doRegist() {
		return CommonConstant.ADM003;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday
	 *            the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/**
	 * @return the birthdayDay
	 */
	public int getBirthdayDay() {
		return birthdayDay;
	}

	/**
	 * @param birthdayDay
	 *            the birthdayDay to set
	 */
	public void setBirthdayDay(int birthdayDay) {
		this.birthdayDay = birthdayDay;
	}

	/**
	 * @return the birthdayMonth
	 */
	public int getBirthdayMonth() {
		return birthdayMonth;
	}

	/**
	 * @param birthdayMonth
	 *            the birthdayMonth to set
	 */
	public void setBirthdayMonth(int birthdayMonth) {
		this.birthdayMonth = birthdayMonth;
	}

	/**
	 * @return the birthdayYear
	 */
	public int getBirthdayYear() {
		return birthdayYear;
	}

	/**
	 * @param birthdayYear
	 *            the birthdayYear to set
	 */
	public void setBirthdayYear(int birthdayYear) {
		this.birthdayYear = birthdayYear;
	}

	/**
	 * @return the codeLevel
	 */
	public String getCodeLevel() {
		return codeLevel;
	}

	/**
	 * @param codeLevel
	 *            the codeLevel to set
	 */
	public void setCodeLevel(String codeLevel) {
		this.codeLevel = codeLevel;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the endDay
	 */
	public int getEndDay() {
		return endDay;
	}

	/**
	 * @param endDay
	 *            the endDay to set
	 */
	public void setEndDay(int endDay) {
		this.endDay = endDay;
	}

	/**
	 * @return the endMonth
	 */
	public int getEndMonth() {
		return endMonth;
	}

	/**
	 * @param endMonth
	 *            the endMonth to set
	 */
	public void setEndMonth(int endMonth) {
		this.endMonth = endMonth;
	}

	/**
	 * @return the endYear
	 */
	public int getEndYear() {
		return endYear;
	}

	/**
	 * @param endYear
	 *            the endYear to set
	 */
	public void setEndYear(int endYear) {
		this.endYear = endYear;
	}

	/**
	 * @return the fullName
	 */
	public String getFullName() {
		return fullName;
	}

	/**
	 * @param fullName
	 *            the fullName to set
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * @return the fullNameKana
	 */
	public String getFullNameKana() {
		return fullNameKana;
	}

	/**
	 * @param fullNameKana
	 *            the fullNameKana to set
	 */
	public void setFullNameKana(String fullNameKana) {
		this.fullNameKana = fullNameKana;
	}

	/**
	 * @return the groupId
	 */
	public int getGroupId() {
		return groupId;
	}

	/**
	 * @param groupId
	 *            the groupId to set
	 */
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	/**
	 * @return the groupName
	 */
	public String getGroupName() {
		return groupName;
	}

	/**
	 * @param groupName
	 *            the groupName to set
	 */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName
	 *            the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @return the lsErrorMessage
	 */
	public List<String> getLsErrorMessage() {
		return lsErrorMessage;
	}

	/**
	 * @param lsErrorMessage
	 *            the lsErrorMessage to set
	 */
	public void setLsErrorMessage(List<String> lsErrorMessage) {
		this.lsErrorMessage = lsErrorMessage;
	}

	/**
	 * @return the lsItemBirthdayDay
	 */
	public List<SelectItem> getLsItemBirthdayDay() {
		return lsItemBirthdayDay;
	}

	/**
	 * @param lsItemBirthdayDay
	 *            the lsItemBirthdayDay to set
	 */
	public void setLsItemBirthdayDay(List<SelectItem> lsItemBirthdayDay) {
		this.lsItemBirthdayDay = lsItemBirthdayDay;
	}

	/**
	 * @return the lsItemBirthdayMonth
	 */
	public List<SelectItem> getLsItemBirthdayMonth() {
		return lsItemBirthdayMonth;
	}

	/**
	 * @param lsItemBirthdayMonth
	 *            the lsItemBirthdayMonth to set
	 */
	public void setLsItemBirthdayMonth(List<SelectItem> lsItemBirthdayMonth) {
		this.lsItemBirthdayMonth = lsItemBirthdayMonth;
	}

	/**
	 * @return the lsItemEndDay
	 */
	public List<SelectItem> getLsItemEndDay() {
		return lsItemEndDay;
	}

	/**
	 * @param lsItemEndDay
	 *            the lsItemEndDay to set
	 */
	public void setLsItemEndDay(List<SelectItem> lsItemEndDay) {
		this.lsItemEndDay = lsItemEndDay;
	}

	/**
	 * @return the lsItemEndMonth
	 */
	public List<SelectItem> getLsItemEndMonth() {
		return lsItemEndMonth;
	}

	/**
	 * @param lsItemEndMonth
	 *            the lsItemEndMonth to set
	 */
	public void setLsItemEndMonth(List<SelectItem> lsItemEndMonth) {
		this.lsItemEndMonth = lsItemEndMonth;
	}

	/**
	 * @return the lsItemEndYear
	 */
	public List<SelectItem> getLsItemEndYear() {
		return lsItemEndYear;
	}

	/**
	 * @param lsItemEndYear
	 *            the lsItemEndYear to set
	 */
	public void setLsItemEndYear(List<SelectItem> lsItemEndYear) {
		this.lsItemEndYear = lsItemEndYear;
	}

	/**
	 * @return the lsItemGroup
	 */
	public List<SelectItem> getLsItemGroup() {
		return lsItemGroup;
	}

	/**
	 * @param lsItemGroup
	 *            the lsItemGroup to set
	 */
	public void setLsItemGroup(List<SelectItem> lsItemGroup) {
		this.lsItemGroup = lsItemGroup;
	}

	/**
	 * @return the lsItemJapan
	 */
	public List<SelectItem> getLsItemJapan() {
		return lsItemJapan;
	}

	/**
	 * @param lsItemJapan
	 *            the lsItemJapan to set
	 */
	public void setLsItemJapan(List<SelectItem> lsItemJapan) {
		this.lsItemJapan = lsItemJapan;
	}

	/**
	 * @return the lsItemStartDay
	 */
	public List<SelectItem> getLsItemStartDay() {
		return lsItemStartDay;
	}

	/**
	 * @param lsItemStartDay
	 *            the lsItemStartDay to set
	 */
	public void setLsItemStartDay(List<SelectItem> lsItemStartDay) {
		this.lsItemStartDay = lsItemStartDay;
	}

	/**
	 * @return the lsItemStartMonth
	 */
	public List<SelectItem> getLsItemStartMonth() {
		return lsItemStartMonth;
	}

	/**
	 * @param lsItemStartMonth
	 *            the lsItemStartMonth to set
	 */
	public void setLsItemStartMonth(List<SelectItem> lsItemStartMonth) {
		this.lsItemStartMonth = lsItemStartMonth;
	}

	/**
	 * @return the lsItemStartYear
	 */
	public List<SelectItem> getLsItemStartYear() {
		return lsItemStartYear;
	}

	/**
	 * @param lsItemStartYear
	 *            the lsItemStartYear to set
	 */
	public void setLsItemStartYear(List<SelectItem> lsItemStartYear) {
		this.lsItemStartYear = lsItemStartYear;
	}

	/**
	 * @return the messageSucess
	 */
	public String getMessageSucess() {
		return messageSucess;
	}

	/**
	 * @param messageSucess
	 *            the messageSucess to set
	 */
	public void setMessageSucess(String messageSucess) {
		this.messageSucess = messageSucess;
	}

	/**
	 * @return the nameLevel
	 */
	public String getNameLevel() {
		return nameLevel;
	}

	/**
	 * @param nameLevel
	 *            the nameLevel to set
	 */
	public void setNameLevel(String nameLevel) {
		this.nameLevel = nameLevel;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the passwordConfirm
	 */
	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	/**
	 * @param passwordConfirm
	 *            the passwordConfirm to set
	 */
	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the startDay
	 */
	public int getStartDay() {
		return startDay;
	}

	/**
	 * @param startDay
	 *            the startDay to set
	 */
	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}

	/**
	 * @return the startMonth
	 */
	public int getStartMonth() {
		return startMonth;
	}

	/**
	 * @param startMonth
	 *            the startMonth to set
	 */
	public void setStartMonth(int startMonth) {
		this.startMonth = startMonth;
	}

	/**
	 * @return the startYear
	 */
	public int getStartYear() {
		return startYear;
	}

	/**
	 * @param startYear
	 *            the startYear to set
	 */
	public void setStartYear(int startYear) {
		this.startYear = startYear;
	}

	/**
	 * @return the strBirthday
	 */
	public String getStrBirthday() {
		return strBirthday;
	}

	/**
	 * @param strBirthday
	 *            the strBirthday to set
	 */
	public void setStrBirthday(String strBirthday) {
		this.strBirthday = strBirthday;
	}

	/**
	 * @return the strEndDate
	 */
	public String getStrEndDate() {
		return strEndDate;
	}

	/**
	 * @param strEndDate
	 *            the strEndDate to set
	 */
	public void setStrEndDate(String strEndDate) {
		this.strEndDate = strEndDate;
	}

	/**
	 * @return the strStartDate
	 */
	public String getStrStartDate() {
		return strStartDate;
	}

	/**
	 * @param strStartDate
	 *            the strStartDate to set
	 */
	public void setStrStartDate(String strStartDate) {
		this.strStartDate = strStartDate;
	}

	/**
	 * @return the tel
	 */
	public String getTel() {
		return tel;
	}

	/**
	 * @param tel
	 *            the tel to set
	 */
	public void setTel(String tel) {
		this.tel = tel;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
	}

	/**
	 * @return the totalStr
	 */
	public String getTotalStr() {
		return totalStr;
	}

	/**
	 * @param totalStr
	 *            the totalStr to set
	 */
	public void setTotalStr(String totalStr) {
		this.totalStr = totalStr;
	}

	/**
	 * @return the userId
	 */
	public int getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * @return the lsItemBirthdayYear
	 */
	public List<SelectItem> getLsItemBirthdayYear() {
		return lsItemBirthdayYear;
	}

	/**
	 * @param lsItemBirthdayYear
	 *            the lsItemBirthdayYear to set
	 */
	public void setLsItemBirthdayYear(List<SelectItem> lsItemBirthdayYear) {
		this.lsItemBirthdayYear = lsItemBirthdayYear;
	}

}
